﻿using HBase;
using hCommand; 
using Net.http;
using Persistence;
using System;
using System.Collections.Generic;
using System.ComponentModel.DataAnnotations;
using System.Linq;
using System.Text;
using System.Text.Json.Serialization;
using System.Threading.Tasks;

namespace WebApi
{
    [FormAuthenticationFilter]
    [RoutePrefix("XydErp")]
    public class XydErpInfoController : BaseApiController
    {
        IBParamServer bParamServer = WMSDalFactray.getDal<IBParamServer>();

        [HttpPost, Route("Receive")]
        public XydResult Receive([FromBody] ChanjetEncryptMsg encryptMsg)
        {
            XydResult retObj = null;

            if (encryptMsg != null)
            {
                string enMsg = encryptMsg.GetEncryptMsg();

                String decryptMsg = OpenapiHelper.AesDecrypt(enMsg, "1111111111111111");

                MessageBase message = JsonUtil.toObj<MessageBase>(decryptMsg);
              
                    switch (message.msgType)
                    {
                        case "APP_TEST":
                            retObj = DealTestMsg(message);
                            break;
                        case "APP_TICKET":
                            retObj = DealTicketMsg(message);
                            break;
                        case "TEMP_AUTH_CODE":
                            retObj = DealOrgTempAuthMsg(message);
                            break;
                        case "PAY_ORDER_SUCCESS":
                            retObj = DealOrderPayMsg(message);
                            break;
                        default:
                            retObj = DealBussnessMsg(message);
                            break;
                    }
                
            }
            return retObj;
        }

        private XydResult DealOrgTempAuthMsg(MessageBase message)
        {
            OrgTempAuthContent content = JsonUtil.toObj<OrgTempAuthContent>(message.bizContent.ToString());
            bParamServer.UpdateBParamRemark(200, content.tempAuthCode);
            return ReceiveMsgOK();
        }

        private XydResult DealOrderPayMsg(MessageBase message)
        {
            return ReceiveMsgOK();
        }

        private XydResult DealBussnessMsg(MessageBase message)
        {
            return ReceiveMsgOK();
        }

        private XydResult DealTestMsg(MessageBase message)
        {
            return ReceiveMsgOK();
        }
        private XydResult DealTicketMsg(MessageBase message)
        {
            AppTicketContent content = JsonUtil.toObj<AppTicketContent>(message.bizContent.ToString());
            bParamServer.UpdateBParamRemark(200, content.appTicket);
            return ReceiveMsgOK();
        }
        private XydResult ReceiveMsgOK()
        {
            return XydResult.Ok("success");
        }
    }
}
